package com.suning.sawp.service.impl.oga;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suning.sawp.constants.BrandTypeConstants;
import com.suning.sawp.dto.oga.bi.StaffCateGroup;
import com.suning.sawp.intf.dao.oga.ClerkCateGroupDaoService;
import com.suning.sawp.intf.oga.ClerkCateGroupService;
import com.suning.sawp.po.oga.ClerkCateGroup;

@Service
public class ClerkCateGroupServiceImpl implements ClerkCateGroupService, InitializingBean {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClerkCateGroupServiceImpl.class);

    private List<Map<String, Object>> ALL_BRAND;

    @Autowired
    ClerkCateGroupDaoService clerkCateGroupDaoService;

    @Override
    public List<StaffCateGroup> handleClerkCateGroup(String storeCode, String cateCode, String cateName,
            List<StaffCateGroup> staffList) {

        List<StaffCateGroup> failed = new ArrayList<StaffCateGroup>();
        for (StaffCateGroup staff : staffList) {
            try {

                if ("1".equals(staff.getOpType())) {

                    ClerkCateGroup group = new ClerkCateGroup();
                    group.setCateCode(cateCode);
                    group.setCateName(cateName);
                    group.setStaffId(staff.getStaffId());
                    group.setStaffName(staff.getStaffName());
                    group.setStoreCode(storeCode);

                    boolean result = clerkCateGroupDaoService.insertClerkCateGroup(group);
                    if (!result) {
                        failed.add(staff);
                    }

                } else {

                    int result = clerkCateGroupDaoService.deleteClerkCateGroup(storeCode, cateCode, staff.getStaffId());

                    if (result == 0) {
                        failed.add(staff);
                    }
                }
            } catch (Exception e) {
                failed.add(staff);
                LOGGER.error("handleClerkCateGroup ERROR", e);
            }
        }

        return failed;

    }

    @Override
    public List<Map<String, Object>> queryStoreCateGroups(String storeCode) {
        return clerkCateGroupDaoService.queryClerkCateGroup4Store(storeCode);
    }

    @Override
    public List<Map<String, Object>> queryStoreCateGroupsSize(String storeCode) {

        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        
        List<Map<String, Object>> selecteds = clerkCateGroupDaoService.queryClerkCateGroup4StoreSize(storeCode);

        // 返回全品类数据
        for (Map<String, Object> brand : ALL_BRAND) {
            // 默认为0
            Map<String,Object> temp = new HashMap<String, Object>();
            temp.put("cateCode", brand.get("cateCode"));
            temp.put("cateName", brand.get("cateName"));
            temp.put("size", 0);
            
            // 如果查询的有结果，则将结果中的人数重新覆盖
            for (Map<String, Object> selected : selecteds) {
                if (brand.get("cateCode").equals(selected.get("cateCode"))) {
                    temp.put("size", selected.get("size"));

                }
            }
            result.add(temp);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> queryStaffWithCateCode(String storeCode, String cateCode) {
        return clerkCateGroupDaoService.queryStaffWithCateCode(storeCode, cateCode);
    }

    @Override
    public void afterPropertiesSet() throws Exception {

        ALL_BRAND = new ArrayList<Map<String, Object>>();
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00001.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00001.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00002.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00002.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00003.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00003.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00004.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00004.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00005.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00005.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00006.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00006.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00007.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00007.getCateName());
            }
        });
        ALL_BRAND.add(new HashMap<String, Object>() {
            {
                put("cateCode", BrandTypeConstants.BRAND_TYPE_00013.getCateCode());
                put("cateName", BrandTypeConstants.BRAND_TYPE_00013.getCateName());
            }
        });

    }

}
